package ar.edu.unicen.exa.isistan.archassis.view.rcs;

import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.wb.swt.SWTResourceManager;

import swing2swt.layout.BorderLayout;
import ar.edu.unicen.exa.isistan.archassis.ia.api.db.JavaClass;
import ar.edu.unicen.exa.isistan.archassis.model.ModelManager;
import ar.edu.unicen.exa.isistan.archassis.model.listeners.ISourceStrategyListener;
import ar.edu.unicen.exa.isistan.archassis.rcs.svn.SVNRevisionControlSystem;
import ar.edu.unicen.exa.isistan.archassis.rcs.svn.SVNVersion;
import ar.edu.unicen.exa.isistan.archassis.source.FileSourceStrategy;
import ar.edu.unicen.exa.isistan.archassis.source.ISourceStrategy;
import ar.edu.unicen.exa.isistan.archassis.source.RCSSourceStrategy;
import ar.edu.unicen.exa.isistan.archassis.view.LongOperations;
import ar.edu.unicen.exa.isistan.archassis.view.dialogs.SourceDialog;

public class SVNComposite extends Composite implements ISourceStrategyListener {
	/**
	 * Constante para evitar que se abra el dialogo de elegir versión.
	 * Es "por ahora", hasta que terminemos el desarrollo.
	 */
	private static final boolean forNow = false;
	private static final Color BACKGROUND = SWTResourceManager.getColor(SWT.COLOR_WHITE);
	private static final String LEMPTY= "____________";
	private Label urlText;
	private Label versionFromText;
	private Label versionToText;

	/**
	 * Create the dialog.
	 * @param parent
	 * @param style
	 */
	public SVNComposite(Composite parent) {
		super(parent, SWT.BORDER);
		setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		createContents();
		ModelManager.getInstance().setSourceStrategyListener(this);
	}

	/**
	 * Create contents of the dialog.
	 */
	private void createContents() {
		this.setLayout(new BorderLayout(5, 5));
		
		Label imgLabel = new Label(this, SWT.NONE);
		imgLabel.setBackground(BACKGROUND);
		imgLabel.setImage(SWTResourceManager.getImage(SVNComposite.class, "/img/repository.png"));
		imgLabel.setLayoutData(BorderLayout.WEST);
		
//		Link link = new Link(this, SWT.NONE);
		//Button changeButton = new Button(this, SWT.NONE);
		//changeButton.addSelectionListener(new SelectionAdapter() {
//		link.addSelectionListener(new SelectionAdapter() {
//			@Override
//			public void widgetSelected(SelectionEvent e) {
//				openDialog();
//			}
//		});
//		link.setLayoutData(BorderLayout.EAST);
//		//changeButton.setLayoutData(BorderLayout.EAST);
//		link.setText("<a>cambiar</a>");
//		//changeButton.setText("...");
//		link.setBackground(BACKGROUND);
		
		
		Composite infoComposite = new Composite(this, SWT.NONE);
		infoComposite.setBackground(BACKGROUND);
		infoComposite.setLayoutData(BorderLayout.CENTER);
		infoComposite.setLayout(new FormLayout());
		
		Label urlLabel = new Label(infoComposite, SWT.NONE);			
		urlLabel.setForeground(SWTResourceManager.getColor(0, 0, 0));
		urlLabel.setBackground(BACKGROUND);
		urlLabel.setText("URL");
		
		urlText = new Label(infoComposite, SWT.READ_ONLY);
		urlText.setForeground(SWTResourceManager.getColor(105, 105, 105));
		urlText.setBackground(BACKGROUND);
		
		Label versionFromLabel = new Label(infoComposite, SWT.NONE);
		versionFromLabel.setForeground(SWTResourceManager.getColor(0, 0, 0));
		versionFromLabel.setBackground(BACKGROUND);
		versionFromLabel.setText("Version inicial");
		
		versionFromText = new Label(infoComposite, SWT.READ_ONLY);
		versionFromText.setForeground(SWTResourceManager.getColor(105, 105, 105));
		versionFromText.setBackground(BACKGROUND);
		versionFromText.setText(LEMPTY);
		
		Label versionToLabel = new Label(infoComposite, SWT.NONE);		
		versionToLabel.setForeground(SWTResourceManager.getColor(0, 0, 0));
		versionToLabel.setBackground(BACKGROUND);
		versionToLabel.setText("Version final");
		
		versionToText = new Label(infoComposite, SWT.READ_ONLY);
		versionToText.setForeground(SWTResourceManager.getColor(105, 105, 105));
		versionToText.setBackground(BACKGROUND);
		versionToText.setText(LEMPTY);
		
		FormData fd_urlLabel = new FormData();
		fd_urlLabel.top = new FormAttachment(0, 0);
		fd_urlLabel.left = new FormAttachment(0, 5);
		urlLabel.setLayoutData(fd_urlLabel);
		
		FormData fd_versionToText = new FormData();
		fd_versionToText.top = new FormAttachment(0, 0);
		fd_versionToText.right = new FormAttachment(100, -5);
		versionToText.setLayoutData(fd_versionToText);
		
		FormData fd_versionToLabel = new FormData();
		fd_versionToLabel.top = new FormAttachment(0, 0);
		fd_versionToLabel.right = new FormAttachment(versionToText, -5);		
		versionToLabel.setLayoutData(fd_versionToLabel);
		
		FormData fd_versionFromText = new FormData();
		fd_versionFromText.top = new FormAttachment(0, 0);
		fd_versionFromText.right = new FormAttachment(versionToLabel, -5);	
		versionFromText.setLayoutData(fd_versionFromText);
		
		FormData fd_versionFromLabel = new FormData();
		fd_versionFromLabel.top = new FormAttachment(0, 0);
		fd_versionFromLabel.right = new FormAttachment(versionFromText, -5);	
		versionFromLabel.setLayoutData(fd_versionFromLabel);
		
		FormData fd_urlText = new FormData();
		fd_urlText.top = new FormAttachment(0, 0);
		fd_urlText.left = new FormAttachment(urlLabel, 5);
		fd_urlText.right = new FormAttachment(versionFromLabel, -5);
		urlText.setLayoutData(fd_urlText);
		

		
	}

	public static void openDialog(Shell shell) {
		
		if (forNow) {
			forNow();
		} else {
			SourceDialog vd = new SourceDialog(shell, SWT.BORDER);
			//puto
			ISourceStrategy ss = vd.open();
			ModelManager.getInstance().setSourceStrategy(ss);
			if (ss == null) return;
		}
		
		List<JavaClass> r = LongOperations.getJavaClassList(shell);
		ModelManager.getInstance().setClassesList(r);
		
//		try {
//			//ControlManager.getInstance().process();
//		} catch (RCSException e1) {
//			// TODO Auto-generated catch block
//			e1.printStackTrace();
//		}
	}
	private static void forNow() {
		try {
			SVNRevisionControlSystem svnRCS = new SVNRevisionControlSystem("http://svn.codespot.com/a/eclipselabs.org/svn-human-diff/trunk/");
			
			ISourceStrategy ss = new RCSSourceStrategy(svnRCS, new SVNVersion(60), new SVNVersion(108));
			ModelManager.getInstance().setSourceStrategy(ss);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

//	@Override
//	public void changeRCS(IRevisionControlSystem rcs0) {
//		if (rcs0 == null) {
//			urlText.setText("");
//			return;
//		}
//		SVNRevisionControlSystem rcs = (SVNRevisionControlSystem)rcs0;
//		urlText.setText(rcs.getUserSVNURL());
//		
//	}

//	@Override
//	public void changeVersionFrom(IVersion v) {
//		if (v == null) {
//			versionFromText.setText(LEMPTY);
//			return;
//		}
//		versionFromText.setText(v.getID());
//		
//	}

//	@Override
//	public void changeVersionTo(IVersion v) {
//		if (v == null) {
//			versionToText.setText(LEMPTY);
//			return;
//		}
//		versionToText.setText(v.getID());
//	}

	@Override
	public void change(ISourceStrategy sourceStrategy) {
		if (sourceStrategy == null) {
			urlText.setText("");
			versionFromText.setText(LEMPTY);
			versionToText.setText(LEMPTY);
			return;
		}
		if (sourceStrategy instanceof RCSSourceStrategy) {
			RCSSourceStrategy rcsss = (RCSSourceStrategy) sourceStrategy;
			urlText.setText(((SVNRevisionControlSystem)rcsss.getRcs()).getUserSVNURL());
			versionFromText.setText(rcsss.getVersionFrom().getID());
			versionToText.setText(rcsss.getVersionTo().getID());
		} else if (sourceStrategy instanceof FileSourceStrategy) {
			FileSourceStrategy fss = (FileSourceStrategy) sourceStrategy;
			urlText.setText("file://" + fss.getPath());
			
			versionFromText.setText("");
			versionToText.setText("");
		} else {
			System.err.println("No hay manera de mostrar el ISourceStrategy dado");
		}
		
	}
}
